package com.gsit.expenditure.controller;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.Set;

import javafx.beans.binding.Bindings;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.util.StringConverter;
import javafx.util.converter.DoubleStringConverter;
import javafx.util.converter.IntegerStringConverter;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import com.gsit.expenditure.client.domina.FXMLBase;
import com.gsit.expenditure.client.domina.VehicleBean;
import com.gsit.expenditure.domain.Vehicle;
import com.gsit.expenditure.services.VehicleServices;
import com.gsit.expenditure.utils.AppContextLoader;
import com.gsit.expenditure.utils.Page;
import com.gsit.expenditure.utils.Utils;
import com.gsit.expenditure.utils.ViewHandler;

public class VehicleController extends BaseController implements Initializable{


	@FXML private TextField vehicleNumber;
	@FXML private TextField vehicleName;
	@FXML private TextField vehicleType;
	@FXML private TextField modelName;
	@FXML private TextField engineNumber;
	@FXML private TextField manufacturedYear;
	@FXML private TextField rate;

	@FXML private Button saveButton;

	//ErrorLabels
	@FXML private Label vehiclenoErrorLabel;
	@FXML private Label vehicleNameErrorLabel;
	@FXML private Label vehicleTypeErrorLabel;
	@FXML private Label modelErrorLabel;
	@FXML private Label rateErrorLabel;
	

	
	private VehicleBean vehicleBean;  
	private Vehicle vehicle;
	private VehicleServices vehicleServices;
	private Validator validator;
	
	
	
	@Override
	public void initialize(URL url, ResourceBundle bundle) {
		vehicleServices=AppContextLoader.getBean(VehicleServices.class);
		vehicleBean=new VehicleBean(null);
		bind();
		
	}
	
	
	private void bind(){
		
		Bindings.bindBidirectional(vehicleBean.getVehicleNumber(),vehicleNumber.textProperty());
		Bindings.bindBidirectional(vehicleBean.getVehicleName(),vehicleName.textProperty());
		Bindings.bindBidirectional(vehicleBean.getVehicleType(),vehicleType.textProperty());
		Bindings.bindBidirectional(vehicleBean.getModelName(),modelName.textProperty());
		Bindings.bindBidirectional(vehicleBean.getEngineNumber(),engineNumber.textProperty());
		Bindings.bindBidirectional(vehicleBean.getManufacturedYear(),manufacturedYear.textProperty());
		StringConverter integerConverter=new IntegerStringConverter();
		StringConverter doubleConverter=new DoubleStringConverter();
		
		Bindings.bindBidirectional(rate.textProperty(),vehicleBean.getRate(),doubleConverter);
		
		
	}
	@Override
	@FXML
	public void onSave(){
		
		if(isValidate(vehicleBean.getObject())){
			System.out.println(">>>>>>. save >>>>>>>"+vehicleBean.getVehicleName());
			
			Vehicle vehicle = vehicleBean.getObject();
			vehicle.setCreatedDate(Utils.getCurrentDate());
			vehicle.setModifiedDate(Utils.getCurrentDate());
			//vehicle.setVersion(1L);
			vehicleServices.saveOrupdate(vehicle);
			showVehiclePopup(vehicle);
			
			reload();
		}
	}	
	private void showVehiclePopup(Vehicle vehicle){
		final FXMLBase<SuccessController> fxmlBase =  ViewHandler.createFXMLBase(Page.SUCCESS_MSG);
		fxmlBase.getController().getStateChange().addListener(new ChangeListener<Boolean>() {
			@Override
			public void changed(ObservableValue<? extends Boolean> arg0,Boolean oldVal, Boolean newVal) {
				if(newVal){
					fxmlBase.getDialogStage().hide();
				}
			}
		});
	
		ViewHandler.createDialog(fxmlBase, "Vehicle successfully created");
		fxmlBase.getDialogStage().showAndWait();
		reload();
	}
	
	public Boolean isValidate(Vehicle vehicle){
		clearErrorLabel();
		Boolean result = Boolean.TRUE;
		if(validator == null){
			validator=Validation.buildDefaultValidatorFactory().getValidator();
		}
		clearErrorLabel();
			
		Set<ConstraintViolation<Vehicle>> constraintViolations=validator.validate(vehicle);
			for (ConstraintViolation<Vehicle> constraintViolation : constraintViolations) {
				System.out.println("constraintViolation.getPropertyPath().toString() >>>> "+constraintViolation.getPropertyPath().toString());
				if("vehicleNumber".equalsIgnoreCase(constraintViolation.getPropertyPath().toString())){
					vehiclenoErrorLabel.setText(constraintViolation.getMessage());
					result = Boolean.FALSE;
				}
				if("vehicleName".equalsIgnoreCase(constraintViolation.getPropertyPath().toString())){
					vehicleNameErrorLabel.setText(constraintViolation.getMessage());
					result = Boolean.FALSE;
				}
				if("vehicleType".equalsIgnoreCase(constraintViolation.getPropertyPath().toString())){
					vehicleTypeErrorLabel.setText(constraintViolation.getMessage());
					result = Boolean.FALSE;
				}
				if("modelName".equalsIgnoreCase(constraintViolation.getPropertyPath().toString())){
					modelErrorLabel.setText(constraintViolation.getMessage());
					result = Boolean.FALSE;
				}
				if("rate".equalsIgnoreCase(constraintViolation.getPropertyPath().toString())){
					rateErrorLabel.setText(constraintViolation.getMessage());
					result = Boolean.FALSE;
				}
				
				
				}
			return result;
	}
	
	@Override
	public void onClear(){
		System.out.println(">>>>>>. onClear >>>>>>>");
		vehicleBean.clean();
	}
	
	public void reload(){
		vehicleNumber.setText("");
		vehicleName.setText("");
		vehicleType.setText("");
		modelName.setText("");
		engineNumber.setText("");
		manufacturedYear.setText("");
		rate.setText("");
		
		
	}
	
	public void clearErrorLabel(){
		vehiclenoErrorLabel.setText("");
		vehicleNameErrorLabel.setText("");
		vehicleTypeErrorLabel.setText("");
		modelErrorLabel.setText("");
		rateErrorLabel.setText("");
		
	}
	

}
